<template>
    <bi-table :data="list"  dragsort  @dragsort="dragsort" item-key="group_id"  :loading="loading" height="300">
        <bi-table-column label="名称" name="title"></bi-table-column>
        <bi-table-column label="冷数据" v-slot="{row}" width="100"  >
            <cold-switch :cold="row.cold" :id="row.group_id" :url="{controller:'admin.goodsTag',action:'groupCold'}" :storage="'shop-goods-tag-group'" ></cold-switch>
        </bi-table-column>
        <bi-table-column label="操作" width="130" v-slot="scope">
            <bi-button type="primary" @click="edit(scope.row)">编辑</bi-button>
            <bi-button type="warning" @click="del(scope.row)">删除</bi-button>
        </bi-table-column>
    </bi-table>
</template>

<script>
import editGroupDialog from './edit-group-dialog.vue';
import coldSwitch from '../field/cold-switch.vue';
export default{
    components:{
        coldSwitch
    },
    dialog:{
        title:'标签分组管理',
        width:500,
        buttons:[
            {text:'添加分组',icon:'fa-plus',type:'primary',click:(e,content)=>{
                content.edit();
            }}
        ]
    },
    data(){
        return {
            list:[],
            loading:true
        }
    },
    methods:{
        getList(){
            this.loading = true;
            this.$ajax({
                url:this.$url({action:'groupList'}),
                success:(data)=>{
                    this.list = data;
                    this.loading = false;
                }
            })
        },
        edit(item){
            let title = '添加分组';
            if (item){
                title = '编辑分组';
            }

            this.$dialog({
                title,
                type:'vue',
                content:admin.createApp(editGroupDialog,{item}),
                callback:()=>{
                    this.getList();
                    this.$user.delLocalStorage('shop-goods-tag-group');
                    this.dialog.callback();
                }
            })
        },
        del(item){
            this.$confirm('确定要删除吗',(re)=>{
                this.$ajax({
                    url:this.$url({action:'delGroup'}),
                    data:{
                        group_id:item.group_id
                    },
                    success:(data)=>{
                        this.getList();
                        this.$user.delLocalStorage('shop-goods-tag-group');
                        this.dialog.callback();
                    }
                });
            });
        },
        dragsort(value){
            this.$loading();
            this.$ajax({
                url:this.$url({action:'groupSort'}),
                data:{
                    ids:value
                },
                success:(data)=>{
                    this.$loading(false);
                    this.$user.delLocalStorage('shop-goods-tag-group');
                    this.dialog.callback();
                }
            });
        },
    },
    mounted(){
        this.getList();
    }
}
</script>